import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

public class Client {

	public static void main(String[] pArgs) 
	{
		if(pArgs.length<3)
		{
			System.out.println("usage: java Client host port boardnum");
			return;
		}
	
		try
		{
			Socket lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
			PrintWriter lOut=new PrintWriter(lSocket.getOutputStream());
			BufferedReader lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
	
            lOut.println(pArgs[2]);
            lOut.flush();

 String lLine=lIn.readLine();
            
            
            //create a list of the string
            ArrayList<String> bd = new ArrayList<String>();

            //read number of rows
            int lNumRows=Integer.parseInt(lLine);

            //read each row
            for(int i=0;i<lNumRows;i++)
            {
                lLine=lIn.readLine();
                bd.add(lLine);
                //here, we would store the row somewhere, to build our board
                //in this demo, we just print it
                System.out.println(lLine);
            }
            
            //read number of columns
            int lNumColumns=bd.get(0).length();
            
            //build the board 
            Board board = new Board (lNumRows,lNumColumns);
            Vector<Position> boxes = new Vector<Position>();
            Position initalPlayerPos = new Position();
            
            
            for (int i=0;i<lNumRows;i++){
            	for (int j=0;j<lNumColumns;j++){
            		char ij = bd.get(i).charAt(j);
            		if ( ij == ' '){
            			board.setData(i, j, 0); //0 for normal square (even if it's outside the board)
            		} else if (ij == '#'){
            			board.setData(i, j, 1); //1 for wall square
            		} else if (ij == '$'){
            			// board.setData(i, j, 10); //10 for box square
            			board.setData(i, j, 0);
            			boxes.add(new Position(i,j));
            		} else if (ij == '.'){
            			board.setData(i, j, 2); //2 for target square
            		} else if (ij == '@'){
//            			board.setData(i, j, 20); //20 for player square
            			board.setData(i, j, 0);
            			initalPlayerPos = new Position(i,j);
            		} 
            		System.out.print(board.getData(i, j)+ " ");
            	}
            	System.out.println();
            }
            // create playerNBoxes positions
            PlayerNBoxes playerNBoxes = new PlayerNBoxes(initalPlayerPos,boxes);

            System.out.println(playerNBoxes);
            
            
            //now, we should find a solution to the sokoban

            //we've found our solution (this is actually the solution to board 1)    
            String lMySol="U R R U U L D L L U L L D R R R R L D D R U R U D L L U R";
            //these formats are also valid:
            //String lMySol="URRUULDLLULLDRRRRLDDRURUDLLUR";
            //String lMySol="0 3 3 0 0 2 1 2 2 0 2 2 1 3 3 3 3 2 1 1 3 0 3 0 1 2 2 0 3";

            //send the solution to the server
            lOut.println(lMySol);
            lOut.flush();
    
            //read answer from the server
            lLine=lIn.readLine();
    
            System.out.println(lLine);
		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
	}

}
